package com.blogsoho.taurus.controller.admin;

import com.blogsoho.taurus.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Created by user on 2017/6/6.
 */
@RequestMapping("admin")
public class BaseAdminController {

    @Autowired
    ContentService contentService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    CustomerService customerService;

    @Autowired
    UserService userService;

    @Autowired
    AttachmentService attachmentService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {

                    public void setAsText(String value) {
                        if (StringUtils.isEmpty(value)) {
                            setValue(null);
                        } else {
                            LocalDateTime ldt = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                            Date d = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
                            setValue(d);
                        }
                    }

                }

        );
    }
}
